﻿using AIStudio.Wpf.Demo01.Models;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace AIStudio.Wpf.Demo01.Controls
{
    public class DragThumb : Thumb
    {

        public DragThumb()
        {
            base.DragDelta += new DragDeltaEventHandler(DragThumb_DragDelta);
            base.DragStarted += DragThumb_DragStarted;
            base.DragCompleted += DragThumb_DragCompleted;
        }       

        private SharpBase SharpBase
        {
            get
            {
                return DataContext as SharpBase;
            }
        }


        private void DragThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            GetDesignerCanvas(this)?.Focus();            
        }

        private void DragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
           
        }

        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (SharpBase != null)
            {
                SharpBase.Left += e.HorizontalChange;
                SharpBase.Top += e.VerticalChange;
            }
           
        }

    

        private Canvas GetDesignerCanvas(DependencyObject element)
        {
            while (element != null && !(element is Canvas))
                element = VisualTreeHelper.GetParent(element);

            return element as Canvas;
        }
    }
}
